#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 09/ago/2011

@author: antstud
'''

import datetime
from koro.core.context import Context
from koro.modules.agenda import model


#===============================================================================
# AgendaContext
#===============================================================================
class AgendaContext(Context):
    def __init__(self):
        Context.__init__(self,model.Agenda)
            
    def __calcola_data(self,giorni,avanti=False):
        if giorni==0:
            return datetime.date.today()
        oggi=datetime.date.today().toordinal()
        ordinal=oggi-giorni
        if avanti:
            ordinal=oggi+giorni
        data_rif=datetime.date.fromordinal(ordinal)
        return data_rif
    
    def scaduti(self,giorni=0):
        data_rif=self.__calcola_data(giorni)
        return self.get_objects(model.Agenda.data_scadenza<data_rif,model.Agenda.fatto==False)
    
    def oggi(self):
        data_rif=datetime.date.today()
        return self.get_objects(model.Agenda.data_scadenza==data_rif,model.Agenda.fatto==False)

    def prossimi(self,giorni=0):
        data_rif=self.__calcola_data(giorni,True)
        return self.get_objects(model.Agenda.data_scadenza>data_rif,model.Agenda.fatto==False)